/****************************************************************************
 * javascript for submit content by ajax
 *
 * version : v0.1 
 *
 * base on : jquery 1.10.2
 *           bootstrap v3.0
 *           chujue_validation.js
 ****************************************************************************/


var Chujue_Request = function() {

    /*
     * INPUT:
     * @method    : "GET" or "POST"(default is POST)
     * @url       : url for request
     * @send_data : data for request
     * @ok_func   : function which will be executed after ajax request successed
     * @ng_func   : function which will be executed after ajax request failed
     * @pre_check : is do validation before ajax request or not(default is true)
     * @is_setmessage : is set result message when request complete(default is true)
     *
     * OUPUT: (json type)
     * @result    : "OK" or "NG"
     * @message   : message to display in the chujue-alert div
     * @ obviously, there also can have other data in json type
     *
     * you could customize ok_func or ng_func to do somethingelse
     * other than only display message
     */
    this.url = "";
    this.method = "POST";
    this.send_data = {};
    this.ok_func = function(data){};
    this.ng_func = function(data){};

    this.pre_check = true;
    this.is_setmessage = true;  // display message or not after ajax request

    var set_message = function(result, message)
    {
        // add alert box to div
        var div_alert =
            '<div class="alert alert-dismissable">' +
            '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' +
            '<strong></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i></i>' +
            '</div>';

        $(".chujue-alert").html(div_alert);
        
        $(".chujue-alert > div").removeClass("alert-success");
        $(".chujue-alert > div").removeClass("alert-danger");

        if (result ==  "OK") {
            $(".chujue-alert > div > strong").text("成功!");
            $(".chujue-alert > div").addClass("alert-success");
        }
        else {
            $(".chujue-alert > div > strong").text("失败!");
            $(".chujue-alert > div").addClass("alert-danger");
        }

        $(".chujue-alert > div > i").text(message);
        // 显示 5 秒钟
        $(".chujue-alert > div").alert();
        setTimeout(function(){$(".chujue-alert > div").fadeOut("slow");}, 5000);
    }

    // do ajax request
    this.ajax_req = function() {

        // do validation before request
        if (this.pre_check) {
            var validator = new Chujue_Validator();
            if (!validator.validation('.cj-validator'))
                return false;
        }

        var _this = this;       // after ajax request, [this] object will be changed
        $.ajax({
            type     : this.method,
            url      : this.url,
            data     : this.send_data,
            dataType : "json", 
        }).done(function(data) {
            // ajax request success
            if (_this.is_setmessage)
                set_message(data["result"], data["message"]);

            // 
            if (data["result"] == "OK")
                _this.ok_func(data);
            else
                _this.ng_func(data);
        }).fail(function() {
            // when ajax request failed 
            // alert("server error!");
        });
        return true ;
    }
}







